gajus / babel-plugin-react-css-modules

Transforms styleName to className using compile time CSS module resolution.
Other
2.05k stars 162 forks source link

`exclude` won't work with `require.context` #65

Open moimikey opened 7 years ago

moimikey commented 7 years ago

Hi,

I'll try to open a PR if I get no where, but I'd like to exclude a file from being transformed, but that file is required via require.context. when debugging, here is the output of stats when it tries to go over the file (config.js) that uses require.context:

PluginPass {
  _c: Map {},
  dynamicData: {},
  plugin:
   Plugin {
     _c: Map {},
     dynamicData: {},
     initialized: true,
     raw: {},
     key: 'react-css-modules',
     manipulateOptions: [Function: manipulateOptions],
     post: undefined,
     pre: undefined,
     visitor:
      { _exploded: {},
        _verified: {},
        ImportDeclaration: [Object],
        JSXElement: [Object],
        Program: [Object] } },
  key: 'react-css-modules',
  file:
   File {
     _c: Map {},
     dynamicData: {},
     pipeline: Pipeline {},
     log:
      Logger {
        filename: '/Users/mshertzberg/Gizmodo/kinja-components/.storybook/config.js',
        file: [Circular] },
     opts:
      { filename: '/Users/mshertzberg/Gizmodo/kinja-components/.storybook/config.js',
        filenameRelative: '/Users/mshertzberg/Gizmodo/kinja-components/.storybook/config.js',
        inputSourceMap: undefined,
        env: [Object],
        mode: undefined,
        retainLines: false,
        highlightCode: true,
        suppressDeprecationMessages: false,
        presets: [],
        plugins: [Object],
        ignore: [],
        only: undefined,
        code: true,
        metadata: true,
        ast: true,
        extends: undefined,
        comments: true,
        shouldPrintComment: undefined,
        wrapPluginVisitorMethod: undefined,
        compact: 'auto',
        minified: false,
        sourceMap: false,
        sourceMaps: false,
        sourceMapTarget: 'config.js',
        sourceFileName: '.storybook/config.js',
        sourceRoot: '/Users/mshertzberg/Gizmodo/kinja-components',
        babelrc: false,
        sourceType: 'module',
        auxiliaryCommentBefore: undefined,
        auxiliaryCommentAfter: undefined,
        resolveModuleSource: undefined,
        getModuleId: undefined,
        moduleRoot: '/Users/mshertzberg/Gizmodo/kinja-components',
        moduleIds: false,
        moduleId: undefined,
        passPerPreset: false,
        parserOpts: false,
        generatorOpts: false,
        basename: 'config' },
     parserOpts:
      { sourceType: 'module',
        sourceFileName: '/Users/mshertzberg/Gizmodo/kinja-components/.storybook/config.js',
        plugins: [Object] },
     pluginVisitors: [ [Object] ],
     pluginPasses: [ [Object] ],
     metadata: { usedHelpers: [], marked: [], modules: [Object] },
     dynamicImportTypes: {},
     dynamicImportIds: {},
     dynamicImports: [],
     declarations: {},
     usedHelpers: {},
     path:
      NodePath {
        parent: [Object],
        hub: [Object],
        contexts: [Object],
        data: {},
        shouldSkip: false,
        shouldStop: false,
        removed: false,
        state: undefined,
        opts: [Object],
        skipKeys: {},
        parentPath: null,
        context: [Object],
        container: [Object],
        listKey: undefined,
        inList: false,
        parentKey: 'program',
        key: 'program',
        node: [Object],
        scope: [Object],
        type: 'Program',
        typeAnnotation: null },
     ast:
      Node {
        type: 'File',
        start: 0,
        end: 722,
        loc: [Object],
        program: [Object],
        comments: [],
        tokens: [Object] },
     code: 'import \'preact/devtools\';\nimport {\n\tconfigure,\n\taddDecorator,\n\twithCentered,\n\twithKnobs,\n\tsetOptions\n} from \'../config/storybook\';\nimport { StyletronHOC } from \'../config/styletron\';\n\nfunction loadStories() {\n\tconst req = require.context(\'../components\', true, /story.js$/);\n\treq.keys().forEach(filename => req(filename));\n}\n\naddDecorator(getStory => <StyletronHOC>{getStory()}</StyletronHOC>);\naddDecorator(withCentered);\naddDecorator(withKnobs);\n\nsetOptions({\n\tname: \'kinja-components\',\n\turl: \'https://github.com/gawkermedia/kinja-components\',\n\tgoFullScreen: false,\n\tshowLeftPanel: true,\n\tshowDownPanel: true,\n\tshowSearchBox: false,\n\tdownPanelInRight: true,\n\tsortStoriesByKind: true\n});\n\nconfigure(loadStories, module);\n',
     shebang: '',
     hub: Hub { file: [Circular], options: undefined },
     scope:
      Scope {
        uid: 16,
        parent: null,
        hub: [Object],
        parentBlock: [Object],
        block: [Object],
        path: [Object],
        labels: [Object],
        references: [Object],
        bindings: [Object],
        globals: [Object],
        uids: {},
        data: {},
        crawling: false } },
  opts:
   { generateScopedName: '[hash:base64:10]',
     webpackHotModuleReloading: true,
     exclude: 'story',
     filetypes: { '.scss': 'postcss-scss' } } }

I would imagine this requires a PR to implement?

Thanks.

gajus commented 7 years ago

Refer to the exclude option.

https://github.com/gajus/babel-plugin-react-css-modules#configuration

moimikey commented 7 years ago

@gajus because of require.context, exclude is not working.

moimikey commented 7 years ago

it's assuming config.js is to be excluded, despite config.js contains require.context with many potential dynamic requires to-be-excluded. i would have assumed that babel-plugin-react-css-modules would need a clause to recursively check through those require.context requires, to which it then can successfully exclude.