Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output dir support #583

Merged
merged 9 commits into from
Mar 7, 2018
Merged

Conversation

wardpeet
Copy link
Contributor

@wardpeet wardpeet commented Mar 2, 2018

What: #180

Why: Prevents polluting src tree when preparing production / static builds.

How: By saving the optional key outputDir in the babel plugin state and doing extra stuff if that key is present at the end of the babel transformation

Checklist:

  • Documentation
  • Tests
  • Code complete

I used the code from pr #396 and just updated it to be complient with the latest version. I changed import style to absolute just to make sure that we can't go wrong here.

@wardpeet wardpeet changed the title Output dir support wip Output dir support Mar 2, 2018
Copy link
Member

@emmatown emmatown left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

@@ -441,6 +442,8 @@ export default function(babel: Babel) {
// path.hub.file.opts.filename !== 'unknown' ||
state.opts.extractStatic

state.outputDir = state.opts.outputDir
Copy link
Member

@emmatown emmatown Mar 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to do this, just use state.opts.outputDir. The only reason it's done for extractStatic is because it used to be determined by other things like as you can see, whether there was a filename passed to babel.

// add emotion.css as an extension
cssFilenameArr.push('emotion.css')

// use absolute path so resolving can't go wrong
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't rely on absolute paths. There are so many times when it would cause problems, for example, distributing an npm package, the imports wouldn't work since they would point to paths that wouldn't exist on a consumers computer. Please use path.relative to get relative paths instead.

@wardpeet
Copy link
Contributor Author

wardpeet commented Mar 6, 2018

so require statements are made relative but disk operations are resolved paths. I've fixed the tests and tested it on a local project.

@tkh44 tkh44 added the merge me! label Mar 6, 2018
@wardpeet
Copy link
Contributor Author

wardpeet commented Mar 6, 2018

@tkh44 what's wrong with babel 7? or nothing we should deal with yet?

@tkh44
Copy link
Member

tkh44 commented Mar 6, 2018 via email

const cssFilename = filenameArr.join('.')
const exists = fs.existsSync(cssFilename)
addSideEffect(path, './' + nodePath.basename(cssFilename))
let cssFilename = path.hub.file.opts.sourceFileName
Copy link
Member

@emmatown emmatown Mar 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you replace path.hub.file.opts.sourceFileName with path.hub.file.opts.generatorOpts ? path.hub.file.opts.generatorOpts.sourceFileName : path.hub.file.opts.sourceFileName the babel 7 error will be fixed.

@codecov
Copy link

codecov bot commented Mar 7, 2018

Codecov Report

Merging #583 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted Files Coverage Δ
packages/babel-plugin-emotion/src/index.js 96.83% <100%> (+0.13%) ⬆️

@tkh44 tkh44 merged commit 4cd357b into emotion-js:master Mar 7, 2018
@tkh44
Copy link
Member

tkh44 commented Mar 7, 2018

Thanks @wardpeet!

@wardpeet wardpeet deleted the feature/babel-output-dir branch March 8, 2018 12:41
@ai
Copy link

ai commented May 29, 2018

Having the problem with new Babel 7 beta release:

Error: Cannot find module './loader.emotion.css'

Seems like it tries to require CSS file from the place near component, but didn’t found it because now Emotion put all files to the same output dir.

Maybe there is a way to enable old behavior to put every file near component's JS file?

@ai
Copy link

ai commented May 29, 2018

Also I afraid to file name conflict. What if we have a/one.js and b/one.js. If I understood the code correctly, they both will produce outputDir + 'one.emotion.css' and one override another.

@ai
Copy link

ai commented May 29, 2018

Maybe we can have old behavior on outputDir?

@wardpeet
Copy link
Contributor Author

The behaviour should be the following
outputdir + /a/one.emotion.css
outputdir + /b/one.emotion.css

@eatsjobs
Copy link

eatsjobs commented Oct 26, 2018

@wardpeet @tkh44 @mitchellhamilton I got a strange result with extract static. I know it's a deprecated feature but at the moment i can't switch to linaria o css-literal-loader. Please(on my knees) is it possibile to only have this fix in the babel plugin?Or a way to workaroud for now?

Problem:
When transpiling with babel cli:

babel src --out-dir esm --ignore 'src/**/*.spec.js,src/**/*.test.js' --copy-files

I got this result:

  • *.css are generated but outside my folder project
  • the import '*.css' in the transpiled code is somenthing like:
import "../../Projects/src/Accordion/AccordionItem.emotion.css";
  • OS: macOS Mojave
  • node version v8.12.0
    "@babel/cli": "^7.1.2",
    "@babel/core": "^7.0.0"
"emotion": "^9.2.8",
    "emotion-theming": "^9.2.6",
    "react": "^16.4.1",
    "react-dom": "^16.4.1",
    "react-emotion": "^9.2.8",
"babel-plugin-emotion": "^9.2.11"
//.babelrc
["emotion",{
                        "autoLabel": true,
                        "hoist": true,
                        "sourceMap": false,
                        "extractStatic": true,
                        "outputDir": "esm"
                    }
                ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants