【问题标题】:Using Django pipeline browserify on Windows在 Windows 上使用 Django 管道 browserify
【发布时间】:2016-07-08 22:43:51
【问题描述】:

我正在尝试关注http://gregblogs.com/how-django-reactjs-and-browserify/。 我经历了一些努力让collectstatic 工作,但它现在运行没有错误。但是,当我尝试加载包含我的 react 组件的页面时,另一个编译过程会启动(我认为 collectstatic 将预处理所有内容,并且不需要编译运行时)。它需要一些技巧才能使其立即工作(https://github.com/j0hnsmith/django-pipeline-browserify/issues/14)。但即使在那个补丁之后,不幸的是这个编译错误。虽然命令现在看起来没问题:如果我执行

C:\Users\JohnSmith\node_modules\.bin\browserify.cmd -t babelify --deps C:\Users\JohnSmith\Documents\test\company\static\dashboard\js\react_test_dashboard_widget.browserify.js

它运行时没有错误并生成依赖项 JSON。当 Django/Pipeline 将相同的命令作为子进程执行时,它会出错

Error: Cannot find module ' babelify' from 'C:\Users\JohnSmith\Documents\test\company

如何克服?

packages.json sn-p

"dependencies": {
  "babel-cli": "^6.6.5",
  "babel-preset-es2015": "^6.6.0",
  "yuglify": "^0.1.4",
  "babelify": "^7.3.0",
  "browserify": "^13.0.1",
  "jquery": "^2.2.0",
  "react": "^15.2.0"
},
"devDependencies": {
  "babel-plugin-transform-class-properties": "^6.10.2",
  "babel-plugin-transform-react-jsx": "^6.8.0",
  "babel-preset-es2016": "^6.11.0",
  "babel-preset-react": "^6.11.1"
}

要求sn-p:

...
django-pipeline==1.6.6
django-pipeline-browserify==0.4.1
futures==3.0.5
...

一些设置(顺便说一句https://github.com/j0hnsmith/django-pipeline-browserify/issues/15):

PIPELINE["CSS_COMPRESSOR"] = "pipeline.compressors.NoopCompressor"
PIPELINE["JS_COMPRESSOR"] = "pipeline.compressors.NoopCompressor"
PIPELINE['SASS_BINARY'] = 'C:\\Ruby22-x64\\bin\\sass.bat'
PIPELINE['BABEL_BINARY'] = 'c:\\Users\\JohnSmith\\node_modules\\.bin\\babel.cmd'
PIPELINE['BROWSERIFY_BINARY'] = 'c:\\Users\\JohnSmith\\node_modules\\.bin\\browserify.cmd'
PIPELINE_BROWSERIFY_BINARY = PIPELINE['BROWSERIFY_BINARY']

if DEBUG:
    PIPELINE["BROWSERIFY_ARGUMENTS"] = '-t babelify'
    PIPELINE_BROWSERIFY_ARGUMENTS = PIPELINE["BROWSERIFY_ARGUMENTS"]

(编译器需要最后一个!) 我的系统:Win 10、Python 2.7、Django 1.8

告诉我我还应该指定什么


更新:错误消息来自 Node 本身。请参阅下面的调用堆栈。请注意,这里我尝试显式指定转换 JS 文件而不是模块名称(这在命令行中也很有效,但在应用程序中效果不佳):

CompilerError: ['c:\\Users\\JohnSmith\\node_modules\\.bin\\browserify.cmd', '-t  c:\\Users\\JohnSmith\\Documents\\test\\node_modules\\babelify\\index.js', u'--deps C:\\Users\\JohnSmith\\Documents\\test\\company\\static\\dashboard\\js\\react_test_dashboard_widget.browserify.js'] exit code 1
Error: Cannot find module '  c:\Users\JohnSmith\Documents\test\node_modules\babelify\index.js' from 'C:\Users\JohnSmith\Documents\test\company'
    at c:\Users\JohnSmith\node_modules\resolve\lib\async.js:46:17
    at process (c:\Users\JohnSmith\node_modules\resolve\lib\async.js:173:43)
    at ondir (c:\Users\JohnSmith\node_modules\resolve\lib\async.js:188:17)
    at load (c:\Users\JohnSmith\node_modules\resolve\lib\async.js:69:43)
    at onex (c:\Users\JohnSmith\node_modules\resolve\lib\async.js:92:31)
    at c:\Users\JohnSmith\node_modules\resolve\lib\async.js:22:47
    at FSReqWrap.oncomplete (fs.js:82:15)

这告诉我,问题可能在于 Node 本身捕获了t 参数并且没有将其传递给browserify。肯定这个问题可能很关键:https://github.com/j0hnsmith/django-pipeline-browserify/issues/14

我覆盖了https://github.com/j0hnsmith/django-pipeline-browserify/blob/master/pipeline_browserify/compiler.py#L55

    command = "%s %s %s --deps %s" % (
        getattr(settings, 'PIPELINE_BROWSERIFY_VARS', ''),
        getattr(settings, 'PIPELINE_BROWSERIFY_BINARY', '/usr/bin/env browserify'),
        getattr(settings, 'PIPELINE_BROWSERIFY_ARGUMENTS', ''),
        self.storage.path(infile),
    )

    command = (
        getattr(settings, 'PIPELINE_BROWSERIFY_BINARY', '/usr/bin/env browserify'),
        getattr(settings, 'PIPELINE_BROWSERIFY_ARGUMENTS', ''),
        "--deps %s" % self.storage.path(infile),
    )

'因为管道编译器代码需要元组。通过他的原始代码,它收到一个完整的字符串,但随后将其分解为单个字符,认为它们都是参数,请参阅https://github.com/jazzband/django-pipeline/blob/master/pipeline/compilers/init.py#L108

    argument_list = []
    for flattening_arg in command:
        if isinstance(flattening_arg, string_types):
            argument_list.append(flattening_arg)
        else:
            argument_list.extend(flattening_arg)

这会导致以后的灾难:

CompilerError: [Error 87] The parameter is incorrect

【问题讨论】:

    标签: python django reactjs browserify django-pipeline


    【解决方案1】:

    我的同事也试图让它在 OSX 上运行,但我们放弃了。我们只是将is_outdated 短路以始终返回true。无论如何,is_outdated 在 cmets 中包含一些关于此的信息 (https://github.com/j0hnsmith/django-pipeline-browserify/blob/master/pipeline_browserify/compiler.py#L41):

    “警告:在我看来,仅生成依赖项可能需要与实际编译一样长的时间,这意味着我们最好每次都强制编译。”

    短路的方法是定义你自己的编译器并注册那个。

    PIPELINE['COMPILERS'] = (
    'pipeline.compilers.sass.SASSCompiler',
    # 'pipeline_browserify.compiler.BrowserifyCompiler',
    'company.utils.compilers.BrowserifyCompiler',
    ...
    )
    

    在哪里

    class BrowserifyCompiler(BrowserifyCompiler):
        def is_outdated(self, infile, outfile):
            return True
    

    最后的评论和之前没有人抱怨过这个事实让我想知道项目的状态......

    https://github.com/j0hnsmith/django-pipeline-browserify/issues/14

    【讨论】:

    • 我正在遵循相同的教程,但似乎无法让 collectstatic 工作。我收到以下错误:“django/contrib/staticfiles/management/commands/collectstatic.py”,第 122 行,收集原始路径,已处理路径,在处理器中处理:文件“pipeline/storage.py”,第 24 行,在post_process output_file = package.output_filename File "pipeline/packager.py", line 44, in output_filename return self.config.get('output_filename') AttributeError: 'tuple' object has no attribute 'get'。想知道您是否遇到类似的错误?
    • @Rads 不响铃。我怀疑你的管道配置有问题。这(当您提供元组或未按预期获得元组时)甚至可能是由于缺少逗号或多余的逗号引起的。这在某处看起来像是一个额外的逗号。在问题中发布您的配置并将链接放在这里,以便我可以关注它。或者只是将其放入 Gist 并告诉链接。
    • @Rads 是browserify 参与您的案件还是您只有pipeline?当我将browserify 引入方程式时,我已经有了一个有效的管道配置。
    猜你喜欢
    • 2015-05-31
    • 2010-11-03
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 2013-04-16
    • 2018-06-30
    • 2018-06-29
    • 2015-04-12
    相关资源
    最近更新 更多