diff --git a/src/babel/__integration-tests__/preval-extract/__snapshots__/code-frame.spec.js.snap b/src/babel/__integration-tests__/preval-extract/__snapshots__/code-frame.spec.js.snap index a7c6f4b25..1a1760a7d 100644 --- a/src/babel/__integration-tests__/preval-extract/__snapshots__/code-frame.spec.js.snap +++ b/src/babel/__integration-tests__/preval-extract/__snapshots__/code-frame.spec.js.snap @@ -36,6 +36,6 @@ Error: test 12 | }; 13 | at <>/src/babel/__integration-tests__/__fixtures__/commonjs/utils.js:10:10 -at <>/test.js:5:47 +at <>/test.js:5:58 `; diff --git a/src/babel/__integration-tests__/preval-extract/__snapshots__/exctraction.spec.js.snap b/src/babel/__integration-tests__/preval-extract/__snapshots__/exctraction.spec.js.snap index 31311ba87..de66f4fb3 100644 --- a/src/babel/__integration-tests__/preval-extract/__snapshots__/exctraction.spec.js.snap +++ b/src/babel/__integration-tests__/preval-extract/__snapshots__/exctraction.spec.js.snap @@ -2,32 +2,32 @@ exports[`preval-extract babel plugin with extraction enabled extract styles to a given file 1`] = ` "/* THIS FILE IS AUTOGENERATED. DO NOT EDIT IT DIRECTLY, NOR COMMIT IT TO VERSION CONTROL. */ -._header__nzd8dy{font-size:3em;}" +._header__zkmz8m{font-size:3em;}" `; exports[`preval-extract babel plugin with extraction enabled extract styles to a given file with output directory specified 1`] = ` "/* THIS FILE IS AUTOGENERATED. DO NOT EDIT IT DIRECTLY, NOR COMMIT IT TO VERSION CONTROL. */ -._header__nzd8dy{font-size:3em;}" +._header__zkmz8m{font-size:3em;}" `; exports[`preval-extract babel plugin with extraction enabled should extract all styles to a single file 1`] = ` "/* THIS FILE IS AUTOGENERATED. DO NOT EDIT IT DIRECTLY, NOR COMMIT IT TO VERSION CONTROL. */ -._header__nzd8dy{font-size:3em;}" +._header__zkmz8m{font-size:3em;}" `; exports[`preval-extract babel plugin with extraction enabled should extract all styles to a single file 2`] = ` "/* THIS FILE IS AUTOGENERATED. DO NOT EDIT IT DIRECTLY, NOR COMMIT IT TO VERSION CONTROL. */ -._body__j2fydj{font-weight:bold;}" +._body__zkmz8m{font-weight:bold;}" `; exports[`preval-extract babel plugin with extraction enabled should extract each style to separate file and include it into source file 1`] = ` "/* THIS FILE IS AUTOGENERATED. DO NOT EDIT IT DIRECTLY, NOR COMMIT IT TO VERSION CONTROL. */ -._header__nzd8dy{font-size:3em;}" +._header__jz3lbe{font-size:3em;}" `; exports[`preval-extract babel plugin with extraction enabled should extract each style to separate file and include it into source file 2`] = ` "/* THIS FILE IS AUTOGENERATED. DO NOT EDIT IT DIRECTLY, NOR COMMIT IT TO VERSION CONTROL. */ -._body__j2fydj{font-weight:bold;}" +._body__16a4ra6{font-weight:bold;}" `; diff --git a/src/babel/__integration-tests__/preval-extract/__snapshots__/preval-extract.spec.js.snap b/src/babel/__integration-tests__/preval-extract/__snapshots__/preval-extract.spec.js.snap index 906a96ef6..bcc66eb73 100644 --- a/src/babel/__integration-tests__/preval-extract/__snapshots__/preval-extract.spec.js.snap +++ b/src/babel/__integration-tests__/preval-extract/__snapshots__/preval-extract.spec.js.snap @@ -28,13 +28,13 @@ import css from '<>/build/css.js'; const size = 3; let color = "#ffffff"; -const header = /*linaria-output*/"_header__3r30ar"; +const header = /*linaria-output*/"_header__zkmz8m"; `; exports[`preval-extract babel plugin should preval css with classname from another prevaled css 1`] = ` "padding: 2rem; -._title__1r77qux { +._title__zkmz8m { margin-bottom: 1rem; }" `; diff --git a/src/babel/__integration-tests__/preval-extract/code-frame.spec.js b/src/babel/__integration-tests__/preval-extract/code-frame.spec.js index b2fd40555..ead0ff0ac 100644 --- a/src/babel/__integration-tests__/preval-extract/code-frame.spec.js +++ b/src/babel/__integration-tests__/preval-extract/code-frame.spec.js @@ -6,14 +6,6 @@ import dedent from 'dedent'; import { transpile } from '../__utils__/exec'; describe('preval-extract babel plugin code frames', () => { - beforeEach(() => { - process.env.NODE_ENV = 'production'; - }); - - afterEach(() => { - process.env.NODE_ENV = ''; - }); - it('should build valid code frame for errors while evaling styles', () => { expect(() => { transpile(dedent` diff --git a/src/babel/__integration-tests__/preval-extract/development.spec.js b/src/babel/__integration-tests__/preval-extract/development.spec.js index 6b55071fa..dab627de4 100644 --- a/src/babel/__integration-tests__/preval-extract/development.spec.js +++ b/src/babel/__integration-tests__/preval-extract/development.spec.js @@ -6,44 +6,19 @@ import dedent from 'dedent'; import { transpile } from '../__utils__/exec'; describe('preval-extract babel plugin', () => { - beforeEach(() => { - process.env.NODE_ENV = 'production'; - }); - - afterEach(() => { - process.env.NODE_ENV = ''; - }); - - it('in development should use filename for slug creation', () => { - process.env.BABEL_ENV = ''; - const { code: codeWithSlugFromContent } = transpile(dedent` + it('should use filename for slug creation', () => { + const { code } = transpile( + dedent` const header = css\` font-size: 3em; \`; - `); - - process.env.BABEL_ENV = 'production'; - const { code: codeWithSlugFromFilename } = transpile( - dedent` - const header = css\` - font-size: 3em; - \`; - `, + `, undefined, { filename: path.join(process.cwd(), 'test.js') } ); - const classnameWithSlugFromContent = /header = \/\*.+\*\/'(_header__[a-z0-9]+)'/g.exec( - codeWithSlugFromContent - ); - const classnameWithSlugFromFilename = /header = \/\*.+\*\/'(_header__[a-z0-9]+)'/g.exec( - codeWithSlugFromFilename - ); + const className = /header = \/\*.+\*\/'(_header__[a-z0-9]+)'/g.exec(code); - expect(classnameWithSlugFromContent).not.toBeNull(); - expect(classnameWithSlugFromFilename).not.toBeNull(); - expect(classnameWithSlugFromContent[0]).not.toEqual( - codeWithSlugFromFilename[0] - ); + expect(className).not.toBeNull(); }); }); diff --git a/src/babel/__integration-tests__/preval-extract/errors.spec.js b/src/babel/__integration-tests__/preval-extract/errors.spec.js index 4e33ca14d..d22fb7d59 100644 --- a/src/babel/__integration-tests__/preval-extract/errors.spec.js +++ b/src/babel/__integration-tests__/preval-extract/errors.spec.js @@ -6,14 +6,6 @@ import dedent from 'dedent'; import { transpile } from '../__utils__/exec'; describe('preval-extract babel plugin errors', () => { - beforeEach(() => { - process.env.NODE_ENV = 'production'; - }); - - afterEach(() => { - process.env.NODE_ENV = ''; - }); - it('should throw error if "css" tagged template literal is not assigned to a variable', () => { expect(() => { transpile(dedent` diff --git a/src/babel/__integration-tests__/preval-extract/exctraction.spec.js b/src/babel/__integration-tests__/preval-extract/exctraction.spec.js index b325dde33..a427fba2b 100644 --- a/src/babel/__integration-tests__/preval-extract/exctraction.spec.js +++ b/src/babel/__integration-tests__/preval-extract/exctraction.spec.js @@ -6,14 +6,6 @@ import dedent from 'dedent'; import { extract } from '../__utils__/exec'; describe('preval-extract babel plugin with extraction enabled', () => { - beforeEach(() => { - process.env.NODE_ENV = 'production'; - }); - - afterEach(() => { - process.env.NODE_ENV = ''; - }); - it('should extract all styles to a single file', () => { const filename = path.join(process.cwd(), 'test.js'); const { data: data1, filenames: filenames1 } = extract( diff --git a/src/babel/__integration-tests__/preval-extract/preval-extract.spec.js b/src/babel/__integration-tests__/preval-extract/preval-extract.spec.js index 44fa0f071..d8c7ffe4e 100644 --- a/src/babel/__integration-tests__/preval-extract/preval-extract.spec.js +++ b/src/babel/__integration-tests__/preval-extract/preval-extract.spec.js @@ -5,14 +5,6 @@ import dedent from 'dedent'; import { transpile } from '../__utils__/exec'; describe('preval-extract babel plugin', () => { - beforeEach(() => { - process.env.NODE_ENV = 'production'; - }); - - afterEach(() => { - process.env.NODE_ENV = ''; - }); - it('should not process tagged template if tag is not "css"', () => { const { code } = transpile(dedent` const header = \` diff --git a/src/babel/__integration-tests__/preval-extract/with-commonjs-imports.spec.js b/src/babel/__integration-tests__/preval-extract/with-commonjs-imports.spec.js index ab7dcf397..f10aac688 100644 --- a/src/babel/__integration-tests__/preval-extract/with-commonjs-imports.spec.js +++ b/src/babel/__integration-tests__/preval-extract/with-commonjs-imports.spec.js @@ -5,14 +5,6 @@ import dedent from 'dedent'; import { transpile } from '../__utils__/exec'; describe('preval-extract babel plugin with commonjs imports', () => { - beforeEach(() => { - process.env.NODE_ENV = 'production'; - }); - - afterEach(() => { - process.env.NODE_ENV = ''; - }); - it('should preval imported constants ', () => { const { code, getCSSForClassName } = transpile(dedent` const constants = require("./src/babel/__integration-tests__/__fixtures__/commonjs/constants.js"); diff --git a/src/babel/__integration-tests__/preval-extract/with-es-imports.spec.js b/src/babel/__integration-tests__/preval-extract/with-es-imports.spec.js index 92d2f4eac..58e115321 100644 --- a/src/babel/__integration-tests__/preval-extract/with-es-imports.spec.js +++ b/src/babel/__integration-tests__/preval-extract/with-es-imports.spec.js @@ -5,14 +5,6 @@ import dedent from 'dedent'; import { transpile } from '../__utils__/exec'; describe('preval-extract babel plugin with ES imports', () => { - beforeEach(() => { - process.env.NODE_ENV = 'production'; - }); - - afterEach(() => { - process.env.NODE_ENV = ''; - }); - it('should preval default export', () => { const { code, getCSSForClassName } = transpile(dedent` import constants from "./src/babel/__integration-tests__/__fixtures__/esm/constants.js"; diff --git a/src/babel/__integration-tests__/preval-extract/with-function-calls.spec.js b/src/babel/__integration-tests__/preval-extract/with-function-calls.spec.js index 0b3a61972..c1884f7b7 100644 --- a/src/babel/__integration-tests__/preval-extract/with-function-calls.spec.js +++ b/src/babel/__integration-tests__/preval-extract/with-function-calls.spec.js @@ -5,14 +5,6 @@ import dedent from 'dedent'; import { transpile } from '../__utils__/exec'; describe('preval-extract babel plugin with function calls', () => { - beforeEach(() => { - process.env.NODE_ENV = 'production'; - }); - - afterEach(() => { - process.env.NODE_ENV = ''; - }); - it('should preval with function call inside an expression', () => { const { code, getCSSForClassName } = transpile(dedent` const constants = require("./src/babel/__integration-tests__/__fixtures__/commonjs/constants.js"); diff --git a/src/babel/__integration-tests__/preval-extract/with-function-declarations.spec.js b/src/babel/__integration-tests__/preval-extract/with-function-declarations.spec.js index 2b7487ef5..d6971b466 100644 --- a/src/babel/__integration-tests__/preval-extract/with-function-declarations.spec.js +++ b/src/babel/__integration-tests__/preval-extract/with-function-declarations.spec.js @@ -5,14 +5,6 @@ import dedent from 'dedent'; import { transpile } from '../__utils__/exec'; describe('preval-extract babel plugin with function delcarations/expressions', () => { - beforeEach(() => { - process.env.NODE_ENV = 'production'; - }); - - afterEach(() => { - process.env.NODE_ENV = ''; - }); - it('should preval with function declaration', () => { const { code, getCSSForClassName } = transpile(dedent` function getConstants() { diff --git a/src/babel/__integration-tests__/preval-extract/with-plain-objects.spec.js b/src/babel/__integration-tests__/preval-extract/with-plain-objects.spec.js index fdc970bc6..5f7a4149c 100644 --- a/src/babel/__integration-tests__/preval-extract/with-plain-objects.spec.js +++ b/src/babel/__integration-tests__/preval-extract/with-plain-objects.spec.js @@ -5,14 +5,6 @@ import dedent from 'dedent'; import { transpile } from '../__utils__/exec'; describe('preval-extract babel plugin with plain objects', () => { - beforeEach(() => { - process.env.NODE_ENV = 'production'; - }); - - afterEach(() => { - process.env.NODE_ENV = ''; - }); - it('should preval styles with shallow object', () => { const { code, getCSSForClassName } = transpile(dedent` const constants = { diff --git a/src/babel/preval-extract/__tests__/prevalStyles.spec.js b/src/babel/preval-extract/__tests__/prevalStyles.spec.js index 2cf8e75e2..d3edd3711 100644 --- a/src/babel/preval-extract/__tests__/prevalStyles.spec.js +++ b/src/babel/preval-extract/__tests__/prevalStyles.spec.js @@ -51,7 +51,6 @@ function runAssertions(expectedReplacement, source = 'css`color: #ffffff`') { } function clearMocks() { - process.env.NODE_ENV = ''; getReplacement.mockClear(); instantiateModule.mockClear(); clearLocalModulesFromCache.mockClear(); @@ -77,10 +76,4 @@ describe('preval-extract/prevalStyles', () => { "css.named('header', 'filename.js')`color: #ffffff`" ); }); - - it('should eval styles and replace css with class name from filename', () => { - process.env.NODE_ENV = 'production'; - runAssertions("css.named('header')`color: #ffffff`"); - process.env.NODE_ENV = ''; - }); }); diff --git a/src/babel/preval-extract/index.js b/src/babel/preval-extract/index.js index 16fc4a782..7399f7edf 100644 --- a/src/babel/preval-extract/index.js +++ b/src/babel/preval-extract/index.js @@ -15,7 +15,7 @@ import { isLinariaTaggedTemplate, isExcluded, } from './validators'; -import { getSelfBinding } from './utils'; +import { getSelfBinding, relativeToCwd } from './utils'; import prevalStyles from './prevalStyles'; import resolveSource from './resolveSource'; import extractStyles from './extractStyles'; @@ -72,7 +72,7 @@ export default (babel: BabelCore) => { token.value.trim() === 'linaria-preval' ); state.foundLinariaTaggedLiterals = false; - state.filename = state.file.opts.filename; + state.filename = relativeToCwd(state.file.opts.filename); }, exit(path: NodePath<*>, state: State) { if (state.skipFile) { diff --git a/src/babel/preval-extract/prevalStyles.js b/src/babel/preval-extract/prevalStyles.js index ae896808e..363f23135 100644 --- a/src/babel/preval-extract/prevalStyles.js +++ b/src/babel/preval-extract/prevalStyles.js @@ -39,26 +39,16 @@ export default function( state: State, requirements: RequirementSource[] ) { - const env = process.env.NODE_ENV || process.env.BABEL_ENV; - const { name } = path.scope.generateUidIdentifier(title); const replacement = getReplacement([ ...requirements, { code: `module.exports = ${path .getSource() - .replace( - /css(?!\.named)/g, - env === 'production' - ? `css.named('${name}')` - : `css.named('${name}', '${state.filename}')` - ) + .replace(/css(?!\.named)/g, `css.named('${name}', '${state.filename}')`) .replace( /css\.named\(([^,]+)\)/, - (input, customName) => - env === 'production' - ? input - : `css.named(${customName}, '${state.filename}')` + (input, customName) => `css.named(${customName}, '${state.filename}')` )}`, loc: path.node.loc.start, },